<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="{{asset('lib/layui-v2.6.3/css/layui.css')}}" media="all">
    <link rel="stylesheet" href="{{asset('css/public.css')}}" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">客户名称</label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="required" lay-reqtext="用户名不能为空" placeholder="请输入客户名称" value="{{$name}}" class="layui-input">
            <input type="hidden" name="id" value="{{$id}}">
            <tip>不超过40个汉字。</tip>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">代理级别</label>
        <div class="layui-input-block">
            <input type="radio" name="agent_level_id" value="1" title="一级代理" @if ($agent_level_id==='一级代理') checked @endif>
            <input type="radio" name="agent_level_id" value="2" title="二级代理" @if ($agent_level_id==='二级代理') checked @endif>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">姓名</label>
        <div class="layui-input-block">
            <input type="text" name="realname" lay-verify="required" placeholder="请输入联系人姓名" value="{{$realname}}" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">手机</label>
        <div class="layui-input-block">
            <input type="number" name="mob" lay-verify="required" lay-reqtext="手机不能为空" placeholder="请输入手机" value="{{$mob}}" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item" id="area-picker">
        <label class="layui-form-label required">地址</label>
                <div class="layui-input-inline" style="width: 200px;">
                    <select name="province" class="province-selector" data-value="" lay-filter="province-1" lay-verify="required">

                    </select>
                </div>
                <div class="layui-input-inline" style="width: 200px;">
                    <select name="city" class="city-selector" data-value="" lay-filter="city-1" lay-verify="required">

                    </select>
                </div>
                <div class="layui-input-inline" style="width: 200px;">
                    <select name="country" class="county-selector" data-value="" lay-filter="county-1" lay-verify="required">

                    </select>
                </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">详细地址</label>
        <div class="layui-input-block">
            <input type="text" name="address" lay-verify="required" placeholder="XX小区X号楼X单元888" value="{{$address}}" class="layui-input">
        </div>
    </div>
    <hr class="layui-border-black">
    <div class="layui-form-item">
        <label class="layui-form-label">登录账号</label>
        <div class="layui-input-block">
            <input type="text" name="username" placeholder="请输入登录账号" value="{{$username}}" class="layui-input" disabled>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">登录密码</label>
        <div class="layui-input-block">
            <input type="text" name="password" placeholder="如果不修改登录密码请留空" value="" class="layui-input">
            <tip style="color: red;">如果不修改登录密码请留空</tip>
        </div>
    </div>
    @csrf

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script src="{{asset('lib/layui-v2.6.3/layui.js')}}" charset="utf-8"></script>
<script src="{{asset('js/lay-config.js?v=2.0.0')}}" charset="utf-8"></script>
<script src="{{asset('js/app.js')}}" charset="utf-8"></script>
<script>
        layui.use(['form', 'layarea'], function () {
        var form = layui.form,
            layer = layui.layer,
            layarea = layui.layarea,
            $ = layui.$;

            //三级联动
            layarea.render({
                elem: '#area-picker',
                data: {
                    province: '{{$province}}',
                    city: '{{$city}}',
                    county: '{{$country}}',
                },
                change: function (res) {
                }
            });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            $.ajax({
                type: 'POST',
                url: '/api/agent/update',
                dataType: 'json',
                data: data.field,
                success:function (res) {
                    if (res.code == 200) {
                        layer.closeAll();
                        parent.location.reload();
                        let iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    } else {
                        layer.open({
                            title: '出问题了'
                            ,content: '网络阻塞，请稍后再试'
                        });
                    }
                },
                //处理422
                error:function (err) {
                    if (err.status == 422) {
                        let json = JSON.parse(err.responseText);
                        json = json.errors;
                        let errorInfo = [];
                        for (let item in json) {
                            for (let i = 0; i < json[item].length; i++) {
                                errorInfo.push(json[item][i]);
                            }
                        }
                        let errStr = errorInfo.join('<br>');
                        layer.open({
                            title: '填写错误'
                            ,content: errStr
                        });
                    }
                    return false;
                },
                complete:function () {

                },
            });
            return false;
        });

    });

</script>
</body>
</html>
